package com.maiyuren.dict_page_view_plugin.dictpageview;


import android.content.Context;
import io.flutter.plugin.common.MethodChannel;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class utils {
    public static int dip2px(Context context, float dpValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }

    public static <T> T invokeMethodAndWaitResult(final @NonNull MethodChannel channel,
                                                  final @NonNull String method, final @Nullable Object arguments,
                                                  final @NonNull SyncBaseCallbackResultImpl<T> callback) throws InterruptedException {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                channel.invokeMethod(method, arguments, callback);
            }
        });
        callback.latch.await();
        return callback.result;
    }

    public static String map2string(Map<String, Object> info) {
        JSONObject jsonObject = new JSONObject(info);
        return jsonObject.toString();
    }

    public static Object string2map(String jsonString, boolean isArray) {
        if (isArray) {
            return jsonArrString2map(jsonString);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            // 创建JSONObject
            JSONObject jsonObject = new JSONObject(jsonString);

            // 遍历JSONObject的键
            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = jsonObject.get(key);
                // 如果值是JSONObject，则递归转换
                if (value instanceof JSONObject) {
                    value = string2map(value.toString(), false);
                }
                else if (value instanceof JSONArray) {
                    value = string2map(value.toString(), true);
                }
                map.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    private static Object jsonArrString2map(String jsonString) {
        List<Object> arr = new Vector<>();
        try {
            // 创建JSONObject
            JSONArray jsonObject = new JSONArray(jsonString);
            for(int i = 0; i < jsonObject.length(); ++i) {
                Object item = jsonObject.get(i);
                if (item instanceof JSONObject) {
                    arr.add(string2map(item.toString(), false));
                }
                else if (item instanceof JSONArray) {
                    arr.add(string2map(item.toString(), true));
                }
                else {
                    arr.add(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arr;
    }
}
